package com.gxa.jjty.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.core.service.BaseService;
import com.gxa.jjty.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @author JJuventus
 * @date 2021/11/10 11:25
 */

public abstract class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    Mapper<T> mapper = null;

    @Override
    public T selectById(Serializable id) {
        T t = mapper.selectByPrimaryKey(id);
        return t;
    }

    @Override
    public PageInfo<T> selectByPage(Integer page, Integer size, T... t) throws IllegalAccessException {
        PageHelper.startPage(page,size);
        List<T> ts = null;
        if (t.length>0){
            Example example = new Example(t[0].getClass());
            Example.Criteria criteria = example.createCriteria();
            Class<?> cls = t[0].getClass();
            Field[] declaredFields = cls.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                String name = declaredField.getName();
                Object o = declaredField.get(t[0]);
                if (o != null){
                    criteria.orLike(name,"%"+o.toString()+"%");
                }
            }
            ts = mapper.selectByExample(example);
        }else{
            ts = mapper.selectAll();
        }
        PageInfo<T> list = PageInfo.of(ts);
        return list;
    }

    @Override
    public PageInfo<T> selectByPageAnd(Integer page, Integer size, T... t) throws IllegalAccessException {
        PageHelper.startPage(page,size);
        List<T> ts = null;
        if (t.length>0){
            Example example = new Example(t[0].getClass());
            Example.Criteria criteria = example.createCriteria();
            Class<?> cls = t[0].getClass();
            Field[] declaredFields = cls.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                String name = declaredField.getName();
                Object o = declaredField.get(t[0]);
                if (o != null){
                    criteria.andLike(name,"%"+o.toString()+"%");
                }
            }
            // 逆序排序
            example.orderBy("sort").desc();
            ts = mapper.selectByExample(example);
        }else{
            ts = mapper.selectAll();
        }
        PageInfo<T> list = PageInfo.of(ts);
        return list;
    }



    @Override
    public List<T> selectDescLike(T t) throws IllegalAccessException {
        List<T> ts = null;
        Example example = new Example(t.getClass());
        Example.Criteria criteria = example.createCriteria();
        Class<?> cls = t.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            Object o = declaredField.get(t);
            if (o != null){
                criteria.orLike(name,"%"+o.toString()+"%");
            }
        }
        example.orderBy(declaredFields[0].getName()).desc();
        ts = mapper.selectByExample(example);
        return ts;
    }

    @Override
    public List<T> selectDesc(T t) throws IllegalAccessException {
        List<T> ts = null;
        Example example = new Example(t.getClass());
        Example.Criteria criteria = example.createCriteria();
        Class<?> cls = t.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            Object o = declaredField.get(t);
            if (o != null){
                criteria.orLike(name,o.toString());
            }
        }
        example.orderBy(declaredFields[0].getName()).desc();
        ts = mapper.selectByExample(example);
        return ts;
    }



    @Override
    public PageInfo<T> selectByPageDesc(Integer page, Integer size, T... t) throws IllegalAccessException {
         PageHelper.startPage(page, size);
        List<T> ts = null;
        Example example=null;
        if (t.length>0){
            example = new Example(t[0].getClass());
            Example.Criteria criteria = example.createCriteria();
            Class<?> cls = t[0].getClass();
            Field[] declaredFields = cls.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                String name = declaredField.getName();
                Object o = declaredField.get(t[0]);
                if (o != null){
                    criteria.orLike(name,"%"+o.toString()+"%");
                }
            }
            //实现逆序
            example.orderBy(declaredFields[0].getName()).desc();
            ts = mapper.selectByExample(example);
        }else{
            ts = mapper.selectAll();
        }
        PageInfo<T> list = PageInfo.of(ts);
        return list;
    }



    @Override
    public List<T> select(T t) {
        List<T> ts = mapper.select(t);
        return ts;
    }

    @Override
    public List<T> selectAll() {
        List<T> ts = mapper.selectAll();
        return ts;
    }

    @Override
    public Integer updateById(T t) throws IllegalAccessException {
        int i = mapper.updateByPrimaryKeySelective(t);
        return i;
    }

    @Override
    public Integer deleteById(Serializable id) {
        int i = mapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public T insert(T t) {
        int i = mapper.insertSelective(t);
        return t;
    }

    @Override
    public List<T> selectDesc1(T t) throws IllegalAccessException {
        int i=0;
        List<T> ts = null;
        Example example = new Example(t.getClass());
        Example.Criteria criteria = example.createCriteria();
        Class<?> cls = t.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            System.out.println(name);
            Object o = declaredField.get(t);
            if (o != null&&o.toString().equals("待接单")){
                criteria.andLike(name,o.toString());
            } else if(o != null){
                criteria.orLike(name,"%"+o.toString()+"%");
            }else if(o !=null&&name.equals("orderUser")){
                criteria.andLike(name,"%"+o.toString()+"%");
            }
        }
        example.orderBy(declaredFields[0].getName()).desc();
        ts = mapper.selectByExample(example);
        return ts;
    }

}
